#include "Point2D.h"

Point2D::Point2D() : _x(0), _y(0)
{
}
        
Point2D::Point2D(int x, int y) : _x(x), _y(y)
{
}

double Point2D::Length() const
{
    return sqrt(_x*_x+_y*_y);
}

const int Point2D::GetX() const
{
    return _x;
}

const int Point2D::GetY() const
{
    return _y;
}

Point2D Point2D::operator + (const Point2D& b) const
{
    return Point2D(_x+b._x,_y+b._y);
}

void Point2D::operator += (const Point2D& b)
{
    _x+=b._x;
    _y+=b._y;
}

Point2D Point2D::operator + (const int n) const
{
    return Point2D(_x+n,_y+n);
}

void Point2D::operator += (const int n)
{
    _x+=n;
    _y+=n;
}

void Point2D::operator - ()
{
    _x=-_x;
    _y=-_y;
}

Point2D Point2D::operator - (const Point2D& b) const
{
    return Point2D(_x-b._x,_y-b._y);
}

void Point2D::operator -= (const Point2D& b)
{
    _x-=b._x;
    _y-=b._y;
}

Point2D Point2D::operator - (const int n) const
{
    return Point2D(_x-n,_y-n);
}

void Point2D::operator -= (const int n)
{
    _x-=n;
    _y-=n;
}

Point2D Point2D::operator * (const int n) const
{
    return Point2D(_x*n,_y*n);
}

void Point2D::operator *= (const int n)
{
    _x*=n;
    _y*=n;
}

Point2D Point2D::operator / (const int n) const
{
    return Point2D(_x/n,_y/n);
}

void Point2D::operator /= (const int n)
{
    _x/=n;
    _y/=n;
}

size_t Point2D::SizeOf() const
{
    return sizeof(_x) + sizeof(_y);
}

void Point2D::Serialize(unsigned char* buffer) const
{
    int offset=0;
    memcpy(buffer+offset,&_x,sizeof(_x));
    offset += sizeof(_x);
    memcpy(buffer+offset,&_y,sizeof(_y));
}

Point2D::Point2D(unsigned char* buffer)
{
    int offset=0;
    memcpy(&_x,buffer+offset,sizeof(_x));
    offset += sizeof(_x);
    memcpy(&_y,buffer+offset,sizeof(_y));
}

ostream& operator << (ostream& os, const Point2D& p)
{
    os << '(' << p._x << ',' << p._y << ')';
}

